package com.jiuzhou.guanwang.jzcp.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.bean.ScheduleBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ScheduleAdapter extends BaseAdapter {
    private final Context mContext;
    private List<ScheduleBean> mData;


    public ScheduleAdapter(Context context) {
        this.mContext = context;
    }

    public int getCount() {
        return this.mData == null ? 0 : this.mData.size();
    }

    public Object getItem(int position) {
        return mData.get(position);
    }

    public long getItemId(int position) {
        return (long) position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_schedule, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ScheduleBean lqMatchInfo = (ScheduleBean) this.mData.get(position);
        viewHolder.tvNameA.setText(lqMatchInfo.getTeam_A_name());
        viewHolder.tvNameB.setText(lqMatchInfo.getTeam_B_name());
//        if (!TextUtils.isEmpty(lqMatchInfo.statusDesc)) {
//            viewHolder.tvStatus.setText(lqMatchInfo.statusDesc);
//            if ("直播中".equals(lqMatchInfo.statusDesc)) {
//                viewHolder.tvStatus.setTextColor(this.mContext.getResources().getColor(R.color.green));
//            } else if ("已结束".equals(lqMatchInfo.statusDesc)) {
//                viewHolder.tvStatus.setTextColor(this.mContext.getResources().getColor(R.color.orange));
//            }
//        }
        if(position!=0&&lqMatchInfo.getDate_utc().equals(mData.get(position-1).getDate_utc())){
            viewHolder.rlTime.setVisibility(View.GONE);
        }else{
            viewHolder.rlTime.setVisibility(View.VISIBLE);
        }
        viewHolder.tvStatus.setText(lqMatchInfo.getTVList());
        viewHolder.tvTime.setText(lqMatchInfo.getDate_utc());
        int index = lqMatchInfo.getStart_play().indexOf(" ");
        String hour = lqMatchInfo.getStart_play().substring(index + 1, lqMatchInfo.getStart_play().length());
        viewHolder.tvDesc.setText(hour);
        viewHolder.tv_match_title.setText(lqMatchInfo.getMatch_title());
        if (!TextUtils.isEmpty(lqMatchInfo.getTeam_A_logo())) {
            Glide.with(this.mContext).load(lqMatchInfo.getTeam_A_logo()).into(viewHolder.ivHost);
        }else{
            Glide.with(this.mContext).load("https://img.dongqiudi.com/soccer/data/logo/team/team_default.png").into(viewHolder.ivHost);
        }
        if (!TextUtils.isEmpty(lqMatchInfo.getTeam_B_logo())) {
            Glide.with(this.mContext).load(lqMatchInfo.getTeam_B_logo()).into(viewHolder.ivVisitor);
        }else{
            Glide.with(this.mContext).load("https://img.dongqiudi.com/soccer/data/logo/team/team_default.png").into(viewHolder.ivVisitor);
        }
        return convertView;
    }

    public void setData(List<ScheduleBean> data) {
        this.mData = data;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @BindView(R.id.tv_match)
        TextView tvMatch;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.iv_host)
        ImageView ivHost;
        @BindView(R.id.tv_name_a)
        TextView tvNameA;
        @BindView(R.id.tv_desc)
        TextView tvDesc;
//        @BindView(R.id.iv_tip)
//        ImageView ivTip;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.iv_visitor)
        ImageView ivVisitor;
        @BindView(R.id.tv_name_b)
        TextView tvNameB;
        @BindView(R.id.rl_date)
        RelativeLayout rlTime;
        @BindView(R.id.tv_match_title)
        TextView tv_match_title;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}